package auto;

import util.Sniffer;
import java.util.Date;
import java.util.HashMap;
import rekeningrijdenserver.RekeningRijdenServer;
import tijdserver.TijdServer;
import util.Message;
import util.NetwerkComponent;

/**
 *
 * @author Jonas
 */
public class BoordComputerDummy implements BoordComputer {
    private TijdServer tijdServer;
    private RekeningRijdenServer rekeningRijdenServer;
    private static int IDCounter=0;
    private String ID;
    private GPS attachedGPS;
    private HashMap<Long,Location> locations = new HashMap<Long,Location>();
    private Sniffer sniffy;

    public BoordComputerDummy(TijdServer tijd,RekeningRijdenServer rekening) {
        tijdServer = tijd;
        rekeningRijdenServer = rekening;
        ID = "BC-id"+IDCounter;
        IDCounter++;
        attachedGPS = null;
        sniffy = null;
    }

    public void pushData(Location location, long timestamp) {
        locations.put(timestamp,location);
        System.out.println("Pushed location:"+location+" @ "+new Date(timestamp));
        if(locations.size()%10 == 9){
            HashMap<String,Object> fields = new HashMap<String,Object>();
            fields.put("data", "sign this please");
            Message transmit=new Message(Message.MessageType.DUMMY,fields);
            if(sniffy != null)
                sniffy.readTransmission(transmit,this,tijdServer);
            tijdServer.sendMessage(transmit, this);

            fields.put("data", "I've driven this much");
            transmit = new Message(Message.MessageType.DUMMY,fields);
            if(sniffy != null)
                sniffy.readTransmission(transmit,this,rekeningRijdenServer);
            rekeningRijdenServer.sendMessage(transmit, this);
        }
    }

    public void attachGPS(GPS gps) {
        if(attachedGPS == null){
            attachedGPS = gps;
            System.out.println("GPS "+gps+" attached");
            attachedGPS.subscribe(this);
        }
        else{
            System.out.println("GPS "+attachedGPS+" replaced by "+gps);
            attachedGPS = gps;
        }
    }

    public void detachGPS() {
        String  occupied = (attachedGPS == null)?"":" anymore";
        attachedGPS.unsubscribe();
        attachedGPS = null;
        System.out.println("No gps attached"+occupied);
    }

    public boolean startEngine() {
        if(attachedGPS == null){
            System.out.println("No GPS present");
            return false;
        }
        else{
            attachedGPS.startMoving();
            System.out.println("Engine started");
            return true;
        }

    }

    public void stopEngine() {
        attachedGPS.stopMoving();
        System.out.println("Engine stopped");
    }

    public void sendMessage(Message msg, NetwerkComponent sender) {
        if(sniffy != null)
                sniffy.readTransmission(msg,sender,this);
        System.out.println("Message received:\n"+msg.getData()+"\n\tFrom "+sender);
    }

    public void addSniffer(Sniffer sniffer) {
        sniffy = sniffer;
        System.out.println("Sniffer added");
    }

    public void removeSniffer() {
        sniffy = null;
        System.out.println("Sniffer removed");
    }

    @Override
    public String toString() {
        return ID;
    }

    public boolean checkWithServer(RekeningRijdenServer rrs) {
        return true;
    }

    public RekeningRijdenServer getServer() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
